/**
 * Created by ryansecreat on 2017/12/22.
 */

const co=require("co");
const ElkLogger=require("./app/lib/elktransport");
module.exports=app=>{
    //app.config.coreMiddleware.unshift();  //

    //app.addSingleton("master",(b)=>{name:"ryan"});

    app.curl = co.wrap(app.curl);
    app.sessionStore = class Store {
        constructor(app) {
            this.app = app;
        }
        * get(key) {
            const res = yield this.app.redis.get(key);
            if (!res) return null;
            return JSON.parse(res);
        }

        * set(key, value, maxAge) {
            if (!maxAge) maxAge = 24 * 60 * 60 * 1000;
            value = JSON.stringify(value);
            yield this.app.redis.set(key, value, 'PX', maxAge);
        }

        * destroy(key) {
            yield this.app.redis.del(key);
        }
    };

    class CustomController extends app.Controller {
        success(result) {
            this.ctx.body = {
                success: true,
                result,
            };
        }

        fail(message) {
            this.ctx.body = {
                success: false,
                message,
            };
        }
    }

     app.controller=CustomController;

     app.beforeStart=()=>{

        app.logger.debug("app start");
        app.getLogger("ryanLogger").set("remote",new ElkLogger({level:"INFO",app }));
        app.getLogger("ryanLogger").info("ryan says");
    };
     /*
    app.messenger.on("hello",data=>{
        app.logger.info(`${data.name}发来贺电`);
    });*/
}